package com.redwine.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.redwine.app.R;
import com.redwine.app.net.responses.SupplierTypeCustom;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证运营商业务类型的适配器
 * 作者：gassion on 17/7/27 14:38
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class SupplierTypeAdapter extends BaseAdapter {

    private Context context;

    private List<SupplierTypeCustom> supplierTypeCustomList;


    public SupplierTypeAdapter(Context context) {
        this.context = context;
    }


    /**
     * 设置数据
     *
     * @param datalist
     */
    public void setData(List<String> datalist) {
        supplierTypeCustomList = new ArrayList<>();
        for (int i = 0; i < datalist.size(); i++) {
            SupplierTypeCustom supplierTypeCustom = new SupplierTypeCustom();
            supplierTypeCustom.setSelect(false);
            supplierTypeCustom.setType(datalist.get(i));
            supplierTypeCustomList.add(supplierTypeCustom);
        }
        notifyDataSetChanged();
    }

    public List<SupplierTypeCustom> getData() {
        if (null != supplierTypeCustomList && !supplierTypeCustomList.isEmpty()) {
            return supplierTypeCustomList;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        if (null != supplierTypeCustomList) {
            return supplierTypeCustomList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != supplierTypeCustomList) {
            return supplierTypeCustomList.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.activity_apply_for_supplier_item, parent, false);
        final SupplierTypeCustom supplierTypeCustom = supplierTypeCustomList.get(position);
        final TextView typeName = (TextView) view.findViewById(R.id.supplier_item_tv_type_name);
        typeName.setText(supplierTypeCustom.getType());
        typeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (supplierTypeCustom.isSelect()) {
                    supplierTypeCustom.setSelect(false);
                    typeName.setTextColor(context.getResources().getColor(R.color.gray4));
                    typeName.setBackgroundResource(R.drawable.supplier_type_normal);
                } else {
                    typeName.setTextColor(context.getResources().getColor(R.color.white));
                    typeName.setBackgroundResource(R.drawable.supplier_type_select);
                    supplierTypeCustom.setSelect(true);
                }
            }
        });
        return view;
    }


}
